<template>
  <section>
    <!--工具条：关键字查询-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keywords" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordsQuery">关键字查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <el-table :data="searchMasters" highlight-current-row v-loading="listLoading" style="width: 100%;">
      <el-table-column type="index" width="30">
      </el-table-column>
      <el-table-column prop="title" label="标题" width="100">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="80">
        <!-- 自定义模板：通过scope可以获取到当前行对象：scope.row -->
        <template scope="scope">
          <span style="color: green" v-if="scope.row.state==0">待审核</span>
          <span style="color: red" v-else-if="scope.row.state==1">待处理</span>
          <span style="color: sandybrown" v-else-if="scope.row.state==2">已处理</span>
          <span style="color: gray" v-else-if="scope.row.state==2">已驳回</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="宠物名字" width="120">
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="80" sortable>
      </el-table-column>
      <el-table-column prop="gender" label="性别" width="60">
        <template scope="scope">
          <span v-if="scope.row.gender==1">公</span>
          <span v-else-if="scope.row.gender==0">母</span>
        </template>
      </el-table-column>
      <el-table-column prop="coatColor" label="颜色" width="80">
      </el-table-column>
      <el-table-column prop="resources" label="宠物图片" width="100">
      </el-table-column>
      <el-table-column prop="type.name" label="宠物类型" width="100">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="80" sortable>
      </el-table-column>
      <el-table-column prop="address" label="饲主联系地址" width="200">
      </el-table-column>
      <el-table-column prop="user.username" label="饲主用户名" width="100">
      </el-table-column>
      
      <el-table-column label="操作">
        <template scope="scope">
          <el-button type="success" size="small" @click="goToHandle(scope.$index, scope.row)">审核通过</el-button>
          <el-button type="danger" size="small" @click="goToReject(scope.$index, scope.row)">审核驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页栏工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="totals"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>
    
  </section>
</template>

<script>

export default {
  
  data() {
    return {

      //分页显示所属数据
      searchMasters: [],
      listLoading: false,
      currentPage: 1,
      pageSize: 5,
      totals: 0,

      //高级查询所需数据
      keywords: null,

    }
  },
  methods: {
    
    //审核通过
    goToHandle: function (index, row){
      this.$confirm('确认要将该信息审核通过吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        //开启忙等框
        this.listLoading = true;
        //发送请求
        this.$http.post('/goToHandle',row.id).then((res) => {
          //关闭忙等框
          this.listLoading = false;
          if(res.data.success){
            this.$message({
              message: '审核通过成功',
              type: 'success'
            });
          }else{
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }
          this.getPets();
        }).catch(res => {
          this.$message({
            message: "系统错误，请稍后重试。",
            type: 'error'
          });
        })
      }).catch(() => {
        //取消
      });
    },

    //审核驳回
    goToReject: function (index, row){
      this.$confirm('确认要将该信息审核驳回吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        //开启忙等框
        this.listLoading = true;
        //发送请求
        this.$http.post('/goToReject',row.id).then((res) => {
          this.listLoading = false;

          if(res.data.success){
            this.$message({
              message: '审核驳回成功',
              type: 'success'
            });
          }else{
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }
          this.getSearchMasters();
        }).catch(res => {
          this.$message({
            message: "系统错误，请稍后重试。",
            type: 'error'
          });
        })
      }).catch(() => {
      });
    },

    //获取寻主信息列表
    getSearchMasters() {
      let paras = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keywords: this.keywords
      };
      this.listLoading = true;
      this.$http.post("/searchMasterMsg/toAudit",paras).then(res => {
        this.totals = res.data.totals;
        this.searchMasters = res.data.rows;
        this.listLoading = false;
      }).catch(res => {
        this.$message.error("获取数据失败!");
      })
    },

    //关键字查询
    keywordsQuery(){
      this.currentPage = 1;
      this.getSearchMasters();
    },

    //分页查询 - 点击某一页查询
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      //重新发送请求
      this.getSearchMasters();
    },
    
  },

  mounted() {
    //页面一加载查询
    this.getSearchMasters();
  }
}

</script>

<style scoped>

</style>